/**
 * 
 */
package stringProcessing;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ganeshT
 *
 */
/**
 * Print all the strings that match a given pattern from a file.
 */
public class RegexTest {
	public RegexTest() throws Exception {
		// matchesTheString();
		// getAllDigitsFromString();
		  printAllMatchedStringsInFile();
		//
//		Pattern p = Pattern.compile("Java");
//		List<String> inputStrings = new ArrayList<String>();
//		List<String> matched = new ArrayList<String>();
//		inputStrings.add("Java hello");
//		inputStrings.add("Javaaa");
//		inputStrings.add("aaaJavaaa");
//		inputStrings.add("Jvaaa");
//		for (String curStr : inputStrings) {
//			Matcher matcher = p.matcher(curStr);
//			while (matcher.find()) {
//				matched.add(curStr);
//			}
//		}
	}

	private void getAllDigitsFromString() {
		System.out.println("abasdfasdf1 2wasdfasdf9_8asdfasdfz asdfasdfyx7".replaceAll("\\p", ""));

	}

	/**
     * 
     */
	private void matchesTheString() {
		Pattern patt1_smallAtBeg_noIntg = Pattern.compile("[a-z][A-Za-z]");
		Pattern patt1_smallAtBeg_withInt = Pattern.compile("[a-z][A-Za-z0-9]");
		Pattern patt1_smallAtBeg_baalNext = Pattern.compile("[a-z]*");
		Matcher m = patt1_smallAtBeg_baalNext.matcher("Abcd435r349klsdf");
		Matcher m1 = patt1_smallAtBeg_baalNext.matcher("dkf&93_+sdkjf");
		Matcher m2 = patt1_smallAtBeg_baalNext.matcher("Aakdjfklsdfkl");
		Matcher m3 = patt1_smallAtBeg_baalNext.matcher("aAKJKDKLDksdjfklsdfl");
		Matcher m4 = patt1_smallAtBeg_baalNext.matcher("1kdsjfkl");
		Matcher m5 = patt1_smallAtBeg_baalNext.matcher("Abcd435r349klsdf");
		System.out.println(m.matches());
		// List<String> inputStringList = new ArrayList<String>();

	}

	private void printAllMatchedStringsInFile() throws Exception {

		// The RE pattern
		Pattern patt = Pattern.compile("[A-Za-z][a-z]+");
		// A FileReader (see the I/O chapter)
		BufferedReader r = new BufferedReader(new FileReader("reflection.txt"));

		// For each line of input, try matching in it.
		String line;
		while ((line = r.readLine()) != null) {
			// For each match in the line, extract and print it.
			Matcher m = patt.matcher(line);
			while (m.find()) {
				// Simplest method:
				// System.out.println(m.group(0));

				// Get the starting position of the text
				int start = m.start(0);
				// Get ending position
				int end = m.end(0);
				// Print whatever matched.
				// Use CharacterIterator.substring(offset, end);
				System.out.println(line.substring(start, end));
			}
		}

	}

	public static void main(String[] args) throws Exception {
		new RegexTest();
	}
}
